/*
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.dspl.validate.validator;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.google.dspl.model.Concept;
import com.google.dspl.model.DataType;
import com.google.dspl.model.Dataset;
import com.google.dspl.model.Identifier;
import com.google.dspl.model.Slice;
import com.google.dspl.validate.ItemReference;
import com.google.dspl.validate.ValidationContext;
import com.google.dspl.validate.violation.DimensionConceptNoDataViolation;
import com.google.dspl.validate.violation.DimensionConceptProblematicTypeViolation;
import com.google.dspl.validate.violation.Violation;

import java.util.List;
import java.util.Set;

/**
 * {@link Validator} for (local or external) {@link Concept}s that are used as dimensions in slices.
 *
 * @author Maryla Ustarroz Calonge
 */
public class DatasetDimensionsValidator implements Validator<Dataset> {

  @Override
  public List<Violation> validate(Dataset dataset, ValidationContext context) {
    ImmutableList.Builder<Violation> violations = ImmutableList.builder();

    Set<Identifier> dimensionIds = Sets.newHashSet();
    for (Slice slice : dataset.getSlices()) {
      dimensionIds.addAll(slice.getDimensions());
    }

    for (Identifier dimensionId : dimensionIds) {
      Concept concept = context.getDatasetResolver().getConcept(dimensionId);
      if (concept != null) {
        violations.addAll(validateDimension(dimensionId, concept, context));
      }
    }

    return violations.build();
  }

  private List<Violation> validateDimension(
      Identifier conceptId, Concept concept, ValidationContext context) {
    Dataset dataset = context.getDatasetResolver().getDataset(conceptId.getDatasetId());
    ItemReference conceptReference = ItemReference.reference(dataset, concept);
    ImmutableList.Builder<Violation> violations = ImmutableList.builder();

    DataType conceptType =
        context.getDatasetResolver().resolveConceptType(conceptId.getDatasetId(), concept);
    Identifier conceptTable =
        context.getDatasetResolver().resolveConceptTable(conceptId.getDatasetId(), concept);
    if (conceptType != DataType.DATE && conceptTable == null) {
      violations.add(new DimensionConceptNoDataViolation(conceptReference, conceptId));
    }

    if (conceptType != null && conceptType != DataType.STRING && conceptType != DataType.DATE) {
      violations.add(
          new DimensionConceptProblematicTypeViolation(conceptReference, conceptId, conceptType));
    }

    return violations.build();
  }
}
